layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 作业统计管理管理
     */
    var ToolExpressSumTask = {
        tableId: "toolExpressSumTaskTable"
    };

    /**
     * 初始化表格的列
     */	
    ToolExpressSumTask.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'customerName', sort: true, title: '客户'},
            {field: 'v2', sort: true, title: '波次'},
            {field: 'outNumber', sort: true, title: '出库单号'},
            {field: 'orderNumber', sort: true, title: '快递单号'},
            {field: 'v5', sort: true, title: '快递公司'},
            {field: 'arrangeNumber', sort: true, title: '小组编号'},
            {field: 'userName', sort: true, title: '小组人员'},
            {field: 'handover', sort: true, title: '交接次数'},
            {field: 'goodsNumber', sort: true, title: '商品组合', totalRowText: '合计：'},
            {field: 'num', sort: true, title: '组合数量'},
            {field: 'v1', sort: true, title: '作业数量', totalRow: true},
            {field: 'sumNum', sort: true, title: '单量', totalRow: true},
            {field: 'v4', sort: true, title: '重量', totalRow: true},
        ]];
    };

    /**
     * 点击查询按钮
     */
    ToolExpressSumTask.search = function () {
        var queryData = {};
	        queryData['outNumber'] = $("#outNumber4").val();
	        queryData['orderNumber'] = $("#orderNumber4").val();
        table.reload(ToolExpressSumTask.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    ToolExpressSumTask.exportExcel = function () {
        var checkRows = table.checkStatus(ToolExpressSumTask.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };
    
    function GetUrlByParamName(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var URL =  decodeURI(window.location.search);
        var r = URL.substr(1).match(reg);
        if(r!=null){
            //decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码
            return  decodeURI(r[2]);
        };
        return null;
    };
    
    $('#btnRevoke4').click(function () {
    	var checkStatus = table.checkStatus('toolExpressSumTaskTable');
        data = checkStatus.data;
        if (data.length>0) {
            var ids = [];
            for (var i = 0; i < data.length; i++) {
            	ids.push(data[i].id);
			}
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/toolExpressSumTask/btnRevoke?ids="+ids, function (data) {
	                if(data.success){
	                	Feng.success("删除成功!");
						parent.submit = 1
		                table.reload(ToolExpressSumTask.tableId);
	                } else {
	                    Feng.error("删除失败!" + data.message + "!");
	                }
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否删除选中明细?", operation);
		} else {
			layer.alert('请至少选中一条数据');
		}
    });

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ToolExpressSumTask.tableId,
        url: Feng.ctxPath + '/toolExpressSumTask/list?waveId='+Feng.getUrlParam("waveId")+"&goodsNumber="+GetUrlByParamName("goodsNumber")+"&userName="+GetUrlByParamName("userName")+"&v2="+Feng.getUrlParam("v2")+"&customerId="+Feng.getUrlParam("customerId")+"&v3="+GetUrlByParamName("v3")+"&arrangeId="+Feng.getUrlParam("arrangeId"),
        page: true,
        limit: 60,
        height: "full-60",
        toolbar: '#toolbarfrom',
		limits:[40,60,120,240,360,480,1000,1500],
        cellMinWidth: 100,
        cols: ToolExpressSumTask.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ToolExpressSumTask.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ToolExpressSumTask.exportExcel();
    });
});
